package com.yxq.mdt.app.modules.socialcontact.ui.fragment.main;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.socialcontact.adapter.MainSocialCircleAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.MainSocialMyCircleAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.ByPageListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.MainSocialCircleBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialMyCircleContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialMyCirclePresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.CircleRecommendListAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.CircleRecommendMyListAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.CircleTheHomepageAc;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的圈子
 */
public class MainSocialFragment extends BaseFragment<MainSocialMyCirclePresenter> implements MainSocialMyCircleContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.circle_my_all_tv)
    TextView circleMyAllTv;
    @BindView(R.id.circle_my_recycler)
    RecyclerView circleMyRecycler;
    @BindView(R.id.circle_recommend_all_tv)
    TextView circleRecommendAllTv;
    @BindView(R.id.circle_recommend_recycler)
    RecyclerView circleRecommendRecycler;
    @BindView(R.id.circle_my_all)
    TextView circleMyAll;
    @BindView(R.id.circle_my_lint)
    LinearLayout circleMyLint;
    @BindView(R.id.more)
    LinearLayout more;
    private List<MainSocialCircleBean.ListBean> mainSocialCircleBeans;
    private List<MainSocialCircleBean.ListBean> getMainSocialCircleBeans;
    MainSocialCircleAdapter mainSocialCircleAdapter;
    MainSocialMyCircleAdapter mainSocialMyCircleAdapter;

    @Override
    protected int attachLayoutId() {
        return R.layout.main_social_fragment_blank;
    }

    @Override
    protected MainSocialMyCirclePresenter getPresenter() {
        return new MainSocialMyCirclePresenter(this, new MainSocialContactModel());
    }

    @Override
    public void initView(View view) {
        mainSocialCircleBeans = new ArrayList<>();
        getMainSocialCircleBeans = new ArrayList<>();
        //推荐圈子
        circleRecommendRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mainSocialCircleAdapter = new MainSocialCircleAdapter(R.layout.circle_list_item_my_circle, mainSocialCircleBeans);
        circleRecommendRecycler.setAdapter(mainSocialCircleAdapter);
        //我的圈子
        circleMyRecycler.setLayoutManager(new GridLayoutManager(mContext, 2));
        mainSocialMyCircleAdapter = new MainSocialMyCircleAdapter(R.layout.circle_list_item, getMainSocialCircleBeans);
        circleMyRecycler.setAdapter(mainSocialMyCircleAdapter);
    }

    @Override
    public void initData() {
        mPresenter.getMyGroupList(1, 4);
        mPresenter.getRecommendGroup("1", "6");
    }

    public void refresh() {
        mPresenter.getMyGroupList(1, 4);
        mPresenter.getRecommendGroup("1", "6");

    }

    @Override
    public void setData(@Nullable Object data) {

    }


    @Override
    public void onLoadRecommendSuccess(MainSocialCircleBean mainSocialCircleBean) {
        if (mainSocialCircleBean.getList() != null && mainSocialCircleBean.getList().size() > 0) {
            mainSocialCircleAdapter.setNewData(mainSocialCircleBean.getList());
            more.setVisibility(View.VISIBLE);
        } else {
            more.setVisibility(View.GONE);
        }
        mainSocialCircleAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent intent = new Intent(mContext, CircleTheHomepageAc.class);
            intent.putExtra("CircleName", mainSocialCircleAdapter.getItem(position).getGroupname());
            intent.putExtra("id", mainSocialCircleAdapter.getItem(position).getId());
            startActivity(intent);
        });
    }

    @Override
    public void onLoadRecommendFailed() {
    }

    @Override
    public void onLoadMyGroupListFailed() {
    }

    @Override
    public void onLoadMyGroupListSuccess(MainSocialCircleBean mainSocialCircleBean) {
        if (mainSocialCircleBean.getTotal() != 0) {
            circleMyLint.setVisibility(View.GONE);
            circleMyRecycler.setVisibility(View.VISIBLE);
            mainSocialMyCircleAdapter.setNewData(mainSocialCircleBean.getList());
        } else {
            circleMyLint.setVisibility(View.VISIBLE);
            circleMyRecycler.setVisibility(View.GONE);
        }

        mainSocialMyCircleAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, CircleTheHomepageAc.class);
            intent.putExtra("CircleName", mainSocialMyCircleAdapter.getItem(position).getGroupname());
            intent.putExtra("id", mainSocialMyCircleAdapter.getItem(position).getId());
            startActivity(intent);
        });
    }

    @Override
    public void onLoadByPageListSuccess(ByPageListBean byPageListBean) {

    }

    @Override
    public void onLoadByPageListFailed() {

    }

    @OnClick({R.id.circle_my_all_tv, R.id.circle_my_all, R.id.circle_recommend_all_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.circle_my_all_tv:
                Intent intentMy = new Intent(mContext, CircleRecommendMyListAc.class);
                startActivity(intentMy);
                break;
            case R.id.circle_my_all:
            case R.id.circle_recommend_all_tv:
                Intent intentAll = new Intent(mContext, CircleRecommendListAc.class);
                startActivity(intentAll);
                break;
        }
    }

}
